package com.ueip.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ueip.backend.mapper.CityMapper;
import com.ueip.backend.mapper.SchoolMapper;
import com.ueip.backend.pojo.City;
import com.ueip.backend.pojo.School;
import com.ueip.backend.pojo.User;
import com.ueip.backend.service.SchoolService;
import com.ueip.backend.utils.IdentityAuthenticationUtil;
import com.ueip.backend.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Resource
    private SchoolMapper schoolMapper;

    @Resource
    private CityMapper cityMapper;

    @Override
    public Result add(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            Integer pid = Integer.parseInt(data.get("pid"));
            Integer cid = Integer.parseInt(data.get("cid"));
            String name = data.get("name");
            String address = data.get("address");
            if (name == null || name.length() == 0 || name.length() > 50) {
                return Result.error("学校名称过长或过短");
            }
            if (address == null || address.length() == 0 || address.length() > 50) {
                return Result.error("学校地址过长或过短");
            }
            City prov = cityMapper.selectById(pid);
            City city = cityMapper.selectById(cid);
            if (prov == null || city == null) {
                return Result.error("省份或城市不存在");
            }
            if (!Objects.equals(city.getPid(), prov.getId())) {
                return Result.error("该城市所在省份不正确");
            }
            School school = schoolMapper.selectOne(new QueryWrapper<School>().eq("name", name));
            if (school != null) {
                return Result.error("该学校已存在，请核查后再试");
            }
            List<School> schools = schoolMapper.selectList(new QueryWrapper<School>().eq("cid", cid));
            BigInteger maxId = BigInteger.ZERO;
            for (School i : schools) {
                if (BigInteger.ZERO.compareTo(maxId) == 0 || i.getId().compareTo(maxId) > 0) {
                    maxId = i.getId();
                }
            }
            School newSchool = new School(maxId.add(BigInteger.ONE), pid, cid, name, address, new Date());
            schoolMapper.insert(newSchool);
            return Result.ok("success");
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }

    @Override
    public Result deleteById(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            BigInteger id = new BigInteger(data.get("id"));
            School school = schoolMapper.selectById(id);
            if (school == null) {
                return Result.error("该学校信息不存在或已被删除");
            }
            schoolMapper.deleteById(school);
            return Result.ok("success");

        } catch (Exception e) {
            return Result.error("数据有误");
        }

    }

    @Override
    public Result update(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            BigInteger id = new BigInteger(data.get("id"));
            School school = schoolMapper.selectById(id);
            if (school == null) {
                return Result.error("该学校不存在或已被删除");
            }
            String name = data.get("name");
            String address = data.get("address");
            if (name == null || name.length() == 0 || name.length() > 50) {
                return Result.error("学校名称过长或过短");
            }
            if (address == null || address.length() == 0 || address.length() > 50) {
                return Result.error("学校地址过长或过短");
            }
            schoolMapper.updateById(new School(school.getId(), school.getPid(), school.getCid(), name, address, school.getCreateTime()));
            return Result.ok("success");
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }

    @Override
    public Result getAll() {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        return Result.ok(schoolMapper.selectList(null));
    }

    @Override
    public Result selectSchoolsByCid(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            Integer cid = Integer.parseInt(data.get("cid"));
            QueryWrapper<School> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cid", cid);

            return Result.ok(schoolMapper.selectList(queryWrapper));
        } catch (Exception e) {
            return Result.ok("城市id数据有误");
        }
    }

    @Override
    public Result getNameById(Map<String, String> data) {
        User curUser = IdentityAuthenticationUtil.getCurUser();
        try {
            BigInteger id = new BigInteger(data.get("id"));
            School school = schoolMapper.selectById(id);
            if (school == null) {
                return Result.error("改学校已被删除或不存在");
            }
            return Result.ok(school.getName());
        } catch (Exception e) {
            return Result.error("数据有误");
        }
    }
}
